package cn.t.repository.impl.user;

import cn.t.converter.user.DeptInfoConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.user.DeptInfoDO;
import cn.t.persistence.entity.user.DeptInfoPO;
import cn.t.persistence.mapper.user.DeptInfoMapper;
import cn.t.repository.user.DeptInfoRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门信息表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class DeptInfoRepositoryImpl extends RepositoryImpl<DeptInfoMapper, DeptInfoPO, DeptInfoConverter, DeptInfoDO> implements DeptInfoRepository {


    @Override
    public List<DeptInfoDO> findByModel(DeptInfoDO deptInfoDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<DeptInfoPO> list = getWrapper(deptInfoDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<DeptInfoPO> getWrapper(DeptInfoDO deptInfoDO) {
        LambdaQueryChainWrapper<DeptInfoPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(deptInfoDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(deptInfoDO.getGmtCreatedEnd())) {
            wrapper.between(DeptInfoPO::getGmtCreated, deptInfoDO.getGmtCreatedStart(), deptInfoDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(deptInfoDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(deptInfoDO.getGmtModifiedEnd())) {
            wrapper.between(DeptInfoPO::getGmtModified, deptInfoDO.getGmtModifiedStart(), deptInfoDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(deptInfoDO.getFirmId())) {
            wrapper.eq(DeptInfoPO::getFirmId, deptInfoDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(deptInfoDO.getDeptName())) {
            wrapper.eq(DeptInfoPO::getDeptName, deptInfoDO.getDeptName());
        }
        if (!ObjectUtils.isEmpty(deptInfoDO.getParentId())) {
            wrapper.eq(DeptInfoPO::getParentId, deptInfoDO.getParentId());
        }
        if (!ObjectUtils.isEmpty(deptInfoDO.getSort())) {
            wrapper.eq(DeptInfoPO::getSort, deptInfoDO.getSort());
        }
        if (!ObjectUtils.isEmpty(deptInfoDO.getDeptType())) {
            wrapper.eq(DeptInfoPO::getDeptType, deptInfoDO.getDeptType());
        }
        if (!ObjectUtils.isEmpty(deptInfoDO.getRemark())) {
            wrapper.eq(DeptInfoPO::getRemark, deptInfoDO.getRemark());
        }
        if (!ObjectUtils.isEmpty(deptInfoDO.getStatus())) {
            wrapper.eq(DeptInfoPO::getStatus, deptInfoDO.getStatus());
        }
        return wrapper;
    }

    @Override
    public long count(DeptInfoDO deptInfoDO) {
        return getWrapper(deptInfoDO).count();
    }
}
